﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GeneratorLib.DB;
using GeneratorLib.DB.Connectios;
using GeneratorLib.DB.Schemas;

namespace GeneratorLib
{
    public partial class TableHost
    {
        public TableInfo Table { get; private set; }
        public TableInfoAddin TableAddin { get; private set; }
        public List<Relation> Relations { get; private set; }

        public IConnection Connection
        {
            get
            {
                return this.Table.Connection;
            }
        }

        public List<FieldInfo> Fields
        {
            get
            {
                return this.Table.Fields;
            }
        }

        public List<FieldInfoAddin> FieldAddins
        {
            get
            {
                return this.TableAddin.FieldAddins;
            }
        }

        public List<string> ListFieldNames
        {
            get
            {
                return this.TableAddin.ListFields;
            }
        }

        public List<string> SearchFieldNames
        {
            get
            {
                return this.TableAddin.SearchFields;
            }
        }

        public TableHost() { }

        public TableHost(TableInfo tableInfo) 
        {
            SetTableInfo(tableInfo);
        }

        public void SetTableInfo(TableInfo tableInfo)
        {
            this.Table = tableInfo;
            this.TableAddin = this.Connection.FindTableAddin(tableInfo.TableName);

            var res = from s in this.Connection.Relations
                      where
                          s.ForeignTable.TableName.ToLower().CompareTo(tableInfo.TableName.ToLower()) == 0
                      select s;

            this.Relations = res.ToList();

        }


    }
}
